<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="8">
        <div class="grid-content bg-purple">
          <el-card class="box-card">
            <div class="avatar">
              <img src="@/assets/动漫头像.jpeg"
                   alt="">
              <div class="user-info">
                <p class="name">Admin</p>
                <p class="access">超级管理员</p>
              </div>
            </div>
            <hr>
            <div class="login-info">
              <p>上次登录时间:<span>2023/6/25 12:23:32</span></p>
              <p>上次登录地点:<span>背景</span></p>
            </div>
          </el-card>
          <el-card class="card-table"
                   style="height: 550px;">
            <el-table
                :data="tableData"
                style="width: 100%">
              <el-table-column v-for="(value,key) in tableLabel"
                               align="center"
                               :prop="key"
                               :key="key"
                               :label="value">
              </el-table-column>
            </el-table>
          </el-card>
        </div>
      </el-col>
      <el-col :span="16">
        <div class="num">
          <el-card :body-style="{display:'flex',padding:0}"
                   v-for="item in countData"
                   :key="item.name">
            <i class="icon"
               :class="`el-icon-${item.icon}`"
               :style="{background:item.color}"></i>
            <div class="detail">
              <p class="price">￥{{ item.value }}</p>
              <p class="name">{{ item.name }}</p>
            </div>
          </el-card>
        </div>
        <el-card id="line-chart"
                 style="height: 350px;">

        </el-card>
        <div class="graph">
          <el-card id="pie-chart"
                   style="height: 350px;"></el-card>
          <el-card id="bar-chart"
                   style="height: 350px;"></el-card>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import Cookie from "js-cookie";
import {getData} from "@/api";
import * as echarts from 'echarts';

export default {
  data() {
    return {
      tableData: [],
      tableLabel: {
        name: '名称',
        todayBuy: '今日购买',
        monthBuy: '本月购买',
        totalBuy: '总购买'
      },
      orderData: {},//折线图
      videoData: [],
      userData: [],
      countData: [
        {name: '今日支付订单', value: 1234, icon: 'success', color: '#2ec7c9'},
        {name: '今日收藏订单', value: 210, icon: 'star-on', color: '#ffb980'},
        {name: '今日未支付订单', value: 1234, icon: 's-goods', color: '#5ab1ef'},
        {name: '本月支付订单', value: 1234, icon: 'success', color: '#2ec7c9'},
        {name: '本月收藏订单', value: 210, icon: 'star-on', color: '#ffb980'},
        {name: '本月未支付订单', value: 1234, icon: 's-goods', color: '#5ab1ef'},
      ]
    }
  },
  mounted() {


    getData().then(({data}) => {
      const {tableData, orderData, userData, videoData} = data.data
      this.tableData = tableData
      this.orderData = orderData
      this.userData = userData
      this.videoData = videoData
      //获取数据之后初始化图表
      this.createLineChart()
      this.createBarChart()
      this.createPieChart()
    })

  },
  methods: {
    createLineChart() {
      // 获取图表容器元素
      const chartContainer = document.getElementById('line-chart');

      // 基于图表容器创建 ECharts 实例
      const chart = echarts.init(chartContainer);

      // 定义折线图的配置项和数据
      const xData = this.orderData.date
      const yData = this.orderData.data

      /**
       * 设置折线图的数据
       * @type {{}}
       */
      const tempYData = {}
      for (let obj of yData) {
        for (let key in obj) {
          //如果没有这个键，则添加
          if (!(key in tempYData)) {
            tempYData[key] = []
            tempYData[key].push(obj[key])
          }
          tempYData[key].push(obj[key])
        }
      }

      const series = []
      for (const key in tempYData) {
        series.push({
          name: key,
          data: tempYData[key],
          type: 'line'
        })
      }


      const option = {
        tooltip: {
          show: true,
          trigger: 'axis',
          formatter: function (params) {
            let content = '';

            // params 是一个数组，包含提示框中的多个数据项
            params.forEach(function (item) {
              content += item.seriesName + ': ' + item.value + '<br>';
            });

            return content;
          }
        },
        legend: {
          data: tempYData.keys
        },
        xAxis: {
          type: 'category',
          data: xData
        },
        yAxis: {
          type: 'value'
        },
        series: series
      };

      // 使用指定的配置项和数据绘制图表
      chart.setOption(option);
    },
    createPieChart() {
      // 创建图表实例
      const myChart = echarts.init(document.getElementById('pie-chart'));


      // 配置选项
      const option = {
        title: {
          text: '饼状图示例', // 图表标题
          left: 'center' // 标题居中显示
        },
        series: [
          {
            name: '数据', // 系列名称
            type: 'pie', // 饼状图类型
            radius: '50%', // 饼状图半径，默认为圆的一半
            data: this.videoData
          }
        ]
      };

      // 将配置项应用到图表实例
      myChart.setOption(option);

    },
    createBarChart() {
      // 创建图表实例
      const myChart = echarts.init(document.getElementById('bar-chart'));

      const xAxisData = this.userData.map(item => item.date)
      const activeUser = this.userData.map(item => item.active)
      const newUser = this.userData.map(item => item.new)
// 配置选项
      const option = {
        xAxis: {
          type: 'category', // x 轴类型为类目型
          data: xAxisData // x 轴数据
        },
        yAxis: {
          type: 'value' // y 轴类型为数值型
        },
        series: [
          {
            type: 'bar', // 柱状图类型
            data: activeUser // 数据项对应的值
          }, {
            type: 'bar', // 柱状图类型
            data: newUser // 数据项对应的值
          }
        ]
      };

// 将配置项应用到图表实例
      myChart.setOption(option);

    }
  }
}
</script>

<style scoped
       lang="less">

.box-card {

  .avatar {
    display: flex;
    margin: 10px auto 20px;

    img {
      width: 150px;
      height: 150px;
      border-radius: 50%;
      border: 1px solid black;
    }

    .user-info {
      margin: auto;

      .name {
        font-size: 32px;
        margin-bottom: 5px;
      }

      .access {

      }
    }
  }

  .login-info {
    p {
      line-height: 32px;
      font-size: 16px;
      color: #999999;
      margin-left: 10px;

      span {
        color: #999999;
        margin-left: 50px;
      }
    }
  }
}

.card-table {
  margin-top: 30px;
}

.graph {
  display: flex;
  justify-content: space-between;

  .el-card {
    margin-top: 10px;
    height: 260px;
    width: 49%;
  }
}

.num {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;

  .el-card {
    width: 32%;
    margin-bottom: 20px;
  }

  .icon {
    width: 80px;
    height: 80px;
    font-size: 30px;
    color: white;
    text-align: center;
    line-height: 80px;
  }

  .detail {
    display: flex;
    flex-direction: column;
    justify-content: center;
    margin-left: 15px;

    .price {
      font-size: 30px;
      margin-bottom: 10px;
      height: 30px;
      line-height: 30px;
    }

    .name {
      font-size: 14px;
      color: #999999;
      text-align: center;
    }
  }
}
</style>
